﻿//
// Created by wellwei on 2023/12/9.
//

#include <iostream>
#include <iomanip>
#include "BorrowingRecord.h"
#include "Book.h"

BorrowingRecord::BorrowingRecord(const std::string& ID, const std::string& bookISBN, const std::string& readerID,
	const std::string& borrowDate, const std::string& returnDate,
	const std::string& continueDate, const std::string status, int remainingDays,
	int index) {
	this->ID = ID;
	this->bookISBN = bookISBN;
	this->borrowerID = readerID;
	this->borrowDate = borrowDate;
	this->returnDate = returnDate;
	this->continueDate = continueDate;
	this->status = status;
	this->remainingDays = remainingDays;
	this->index = index;
}

BorrowingRecord::BorrowingRecord(const std::string& bookISBN, const std::string& readerID,
	const std::string& borrowDate, int index) {
	this->ID = borrowDate + readerID + std::to_string(index);//std::to_string(index) 将整数索引转换为字符串
	this->bookISBN = bookISBN;
	this->borrowerID = readerID;
	this->borrowDate = borrowDate;

	{// 还书日期默认为借书日期后 30 天
		// 计算还书日期，考虑闰年
		std::string year = borrowDate.substr(0, 4);
		std::string month = borrowDate.substr(5, 2);
		std::string day = borrowDate.substr(8, 2);
		int yearInt = std::stoi(year);
		int monthInt = std::stoi(month);
		int dayInt = std::stoi(day);
		if (monthInt == 2) {
			if (yearInt % 4 == 0 && yearInt % 100 != 0 || yearInt % 400 == 0) {
				dayInt += 29;
			}
			else {
				dayInt += 28;
			}
		}
		else if (monthInt == 4 || monthInt == 6 || monthInt == 9 || monthInt == 11) {
			dayInt += 30;
		}
		else {
			dayInt += 31;
		}
		if (dayInt > 30) {
			dayInt -= 30;
			monthInt++;
		}
		if (monthInt > 12) {
			monthInt -= 12;
			yearInt++;
		}
		this->returnDate = std::to_string(yearInt) + "-" + std::to_string(monthInt) + "-" + std::to_string(dayInt);
	}
	this->continueDate = "";
	this->remainingDays = 30;
	this->index = index;
	this->status = "借阅中";
}

const std::string& BorrowingRecord::getID() const {
	return ID;
}

void BorrowingRecord::setID(const std::string& ID) {
	this->ID = ID;
}

const std::string& BorrowingRecord::getBookISBN() const {
	return bookISBN;
}

void BorrowingRecord::setBookISBN(const std::string& bookISBN) {
	this->bookISBN = bookISBN;
}

const std::string& BorrowingRecord::getBorrowerID() const {
	return borrowerID;
}

void BorrowingRecord::setReaderID(const std::string& readerID) {
	this->borrowerID = readerID;
}

const std::string& BorrowingRecord::getBorrowDate() const {
	return borrowDate;
}

void BorrowingRecord::setBorrowDate(const std::string& borrowDate) {
	this->borrowDate = borrowDate;
}

const std::string& BorrowingRecord::getReturnDate() const {
	return returnDate;
}

void BorrowingRecord::setReturnDate(const std::string& returnDate) {
	this->returnDate = returnDate;
}

const std::string& BorrowingRecord::getContinueDate() const {
	return continueDate;
}

void BorrowingRecord::setContinueDate(const std::string& continueDate) {
	this->continueDate = continueDate;
}

const std::string& BorrowingRecord::getStatus() const {
	return status;
}

void BorrowingRecord::setStatus(const std::string& status) {
	this->status = status;
}

int BorrowingRecord::getRemainingDays() const {
	return remainingDays;
}

void BorrowingRecord::setRemainingDays(int remainingDays) {
	this->remainingDays = remainingDays;
}

int BorrowingRecord::getIndex() const {
	return index;
}

void BorrowingRecord::setIndex(int index) {
	this->index = index;
}

void BorrowingRecord::showInfo(Book book) {
	std::cout << std::setfill(' ') << std::setw(20) << std::left << ID
		<< std::setfill(' ') << std::setw(16) << std::left << book.getName()
		<< std::setfill(' ') << std::setw(18) << std::left << book.getAuthor()
		<< std::setfill(' ') << std::setw(15) << std::left << book.getISBN()
		<< std::setfill(' ') << std::setw(20) << std::left << borrowerID
		<< std::setfill(' ') << std::setw(20) << std::left << borrowDate
		<< std::setfill(' ') << std::setw(20) << std::left << returnDate
		<< std::setfill(' ') << std::setw(20) << std::left << continueDate
		<< std::setfill(' ') << std::setw(10) << std::left << status
		<< std::setfill(' ') << std::setw(10) << std::left << remainingDays
		<< std::endl;
}
